package com.ld.security.server.admin.modules.msg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ld.security.common.mybatis.base.po.PagePo;
import com.ld.security.common.mybatis.base.service.*;
import com.ld.security.common.mybatis.base.vo.PageVo;
import com.ld.security.common.mybatis.util.JdbcUtil;
import com.ld.security.server.admin.modules.msg.constant.MsgGroupNewsUserRelConstant;
import com.ld.security.server.admin.modules.msg.entity.MsgGroupUserRelEntity;
import com.ld.security.server.admin.modules.msg.mapping.MsgGroupNewsUserRelMapping;
import com.ld.security.server.admin.modules.msg.mapping.MsgGroupUserRelMapping;
import com.ld.security.server.admin.modules.msg.service.IMsgGroupService;

import com.ld.security.server.admin.modules.msg.entity.MsgGroupEntity;

import com.ld.security.server.admin.modules.msg.vo.MyGroupMsgVo;
import com.ld.security.server.admin.modules.msg.vo.MyGroupNewsTotalVo;
import com.ld.security.server.admin.modules.msg.vo.MyGroupVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
@Transactional
public class MsgGroupServiceImpl  extends ISuperServiceImpl<MsgGroupEntity>
implements
        IMsgGroupService {

        @Autowired
        MsgGroupUserRelMapping msgGroupUserRelMapping;

        @Override
        public PageVo<MyGroupVo> getMyGroup(PagePo<MyGroupVo> po) {
                return JdbcUtil.page(po);

        }

        @Override
        public PageVo<MyGroupMsgVo> getMyGroupMsgVo(PagePo<MyGroupMsgVo> po) {
                return JdbcUtil.page(po);
        }

        @Override
        public List<MyGroupNewsTotalVo> getMyGroupNewsTotal(String userId) {
                MyGroupNewsTotalVo vo = new MyGroupNewsTotalVo();
                vo.setUserId(userId);
                vo.setMsgStatus(MsgGroupNewsUserRelConstant.msgStatus.no_read);


                //数据初始化
                QueryWrapper qw =  new QueryWrapper<>().eq("user_id",userId);
              List<MsgGroupUserRelEntity> rels =   msgGroupUserRelMapping.all(qw);


              List<MyGroupNewsTotalVo> vals =   JdbcUtil.all(vo);

                Set<String> groupSet= vals.stream().map(v -> v.getGroupName()).collect(Collectors.toSet());

            for (MsgGroupUserRelEntity rel : rels) {
                    if(groupSet.contains(rel.getGroupName())){

                    }else{
                            MyGroupNewsTotalVo q = new MyGroupNewsTotalVo();
                            q.setGroupName(rel.getGroupName());
                            q.setTotal(0);
                            q.setGroupId(rel.getGroupId());
                            vals.add(q);
                    }

            }




              //  vals.addAll(vs);

                return vals;

        }
}
